//
//  GSMoreSetViewController.m
//  GameSky
//
//  Created by lanou on 16/1/21.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//
/**
 *  设置界面
 */
#import "GSMoreSetViewController.h"
#import "GSRootTableViewCell.h"
#import "GSAboutViewController.h"

typedef NS_ENUM(NSInteger, oneSection){
    Font = 0,
    Push,
    CollectPic,
} ;

typedef NS_ENUM(NSInteger, twoSection){
    Story,
    About,
} ;

typedef NS_ENUM(NSInteger, threeSection){
    Evaluate,
} ;

@interface GSMoreSetViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) NSArray *totalArray;

@property (nonatomic, assign) NSInteger number;

@property (nonatomic, strong) GSAboutViewController *aboutVC;

@end

@implementation GSMoreSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.bar.titleLabel.text = @"设置";
    
    self.number = -1;
    
    [self addData];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStyleGrouped)];
    // 夜间模式
    self.view.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.alwaysBounceVertical = NO;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

    
}

- (void)addData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"MoreSet" ofType:@"plist"];
    
    self.totalArray  = [NSArray arrayWithContentsOfFile:path];
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.totalArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = self.totalArray[section];
    
    return array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    NSArray *array = self.totalArray[indexPath.section];
    
    NSString *title = array[indexPath.row];
    
    cell.textLabel.text = title;
    
    NSArray *fontArray = @[@"小",@"中",@"大"];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 50)];
    
    if ([title isEqualToString:@"字体大小"]) {
        
        
        for (int i = 0; i < 3; i++) {
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i * 30, 0, 30, 50)];
            
            label.text = fontArray[i];

            [bgView addSubview:label];
        }
        
        cell.accessoryView = bgView;
        
        NSNumber *fontNumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"tag"];
        
        if (fontNumber == nil) {
            self.number = 1;
            
        }else{
            
            NSInteger i = fontNumber.integerValue - 20;
            
            UILabel *label = (UILabel *)cell.accessoryView.subviews[i];
            
            label.textColor = [UIColor redColor];
            
            self.number = i;
            
        }

        
    }
    
    if ([title isEqualToString:@"接收推送"]) {
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        button.frame = CGRectMake(0, 0, 60, 25);
        
        button.userInteractionEnabled = NO;
        
        NSString *push = [[NSUserDefaults standardUserDefaults] objectForKey:@"Puch"];
        
        if (push != nil) {
            
            [button setImage:[UIImage imageNamed:push] forState:(UIControlStateNormal)];
        }else{
            
            [button setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
        }
        
        
        
        cell.accessoryView = button;
    }
    
    if ([title isEqualToString:@"在文章中使用图集"]) {
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        button.frame = CGRectMake(0, 0, 60, 25);
        
        button.userInteractionEnabled = NO;
        
        NSString *collectPic = [[NSUserDefaults standardUserDefaults] objectForKey:@"CollectPic"];
        
        if (collectPic != nil) {
            
            [button setImage:[UIImage imageNamed:collectPic] forState:(UIControlStateNormal)];
        }else{
            
            [button setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
        }
        
        cell.accessoryView = button;
        
    }
    

    return cell;
 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 立即取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSUserDefaults *deafult = [NSUserDefaults standardUserDefaults];
    
    if (indexPath.section == 0 ) {
        switch (indexPath.row) {
                
            case Font:{
                
                if (self.number >= 2) {
                    
                    self.number = -1;
                }
                
                self.number++;
                
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                
                UILabel *label = (UILabel *)cell.accessoryView.subviews[self.number];
                
                UILabel *label1 = (UILabel *)cell.accessoryView.subviews[2];
                label1.textColor = [UIColor blackColor];
                
                label.textColor = [UIColor redColor];
                
                if (self.number >= 1) {
                    
                    UILabel *label = (UILabel *)cell.accessoryView.subviews[self.number - 1];
                    
                    label.textColor = [UIColor blackColor];
                    
                }
                
                NSNumber *fontTag = [NSNumber numberWithInteger:self.number + 20];
                
                [[NSUserDefaults standardUserDefaults] setObject:fontTag forKey:@"tag"];
                
            }
                break;
                
            case Push:{
                
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                
                UIButton *button = (UIButton *)cell.accessoryView;
                
                if ([button.currentImage isEqual:[UIImage imageNamed:@"off"]]) {
                    
                    [button setImage:[UIImage imageNamed:@"on"] forState:(UIControlStateNormal)];
                    
                    [deafult setObject:@"on" forKey:@"Push"];
                    
                    
                }else{
                    
                    [button setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
                    
                    [deafult setObject:@"off" forKey:@"Push"];
                }
                
            }
                break;
                
            case CollectPic:{
                
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                
                UIButton *button = (UIButton *)cell.accessoryView;
                
                if ([button.currentImage isEqual:[UIImage imageNamed:@"off"]]) {
                    
                    [button setImage:[UIImage imageNamed:@"on"] forState:(UIControlStateNormal)];
                    
                    [deafult setObject:@"on" forKey:@"CollectPic"];
                    
                }else{
                    
                    [button setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
                    
                    [deafult setObject:@"off" forKey:@"CollectPic"];
                    
                }
                
            }
                
                break;

        }
    }
    
    if (indexPath.section == 1) {
        
        switch (indexPath.row) {

            case Story:
                
                break;
                
            case About:{
                
                self.aboutVC = [[GSAboutViewController alloc]init];
                
                
                self.aboutVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
                
                
                [[UIApplication sharedApplication].keyWindow addSubview:self.aboutVC.view];
                
                
                [UIView animateWithDuration:0.5 animations:^{
                    
                    self.aboutVC.view.transform = CGAffineTransformTranslate(self.aboutVC.view.transform, -kScreenWidth, 0);
                    
                }];
                
                
                
            }
                
                break;
    
        }
    }
    
    if (indexPath.section == 2) {
        
        switch (indexPath.row) {

            case Evaluate:
                
                break;
            
            break;
        }
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

@end
